use "Output Data\contracts_analysis_public.dta", clear

rename *, lower
drop if year < 2000

egen comp_agency = group(companyid agencyid)
egen agency_year = group(agencyid year)
egen comp_year = group(companyid year)
egen agency_ind = group(agencyid naics)
egen agency_ind_year = group(agencyid naics year)
bysort agencyid naics: egen ind_contracts = max(n_contracts)

tsset comp_agency year

gen contract_flag = 0
replace contract_flag = 1 if n_contracts > 0 & !missing(n_contracts)

gen event = 0
replace event = 1 if appointment == 1
replace event = 0 if year < 2000
replace event = 0 if year > 2018
bysort comp_agency: egen first_event_drop = min(year) if event == 1
bysort comp_agency: egen first_event = mean(first_event_drop)
drop first_event_drop
bysort comp_agency: egen first_year = min(year) if !missing(first_event)
replace first_year = 2000 if first_year < 2000
replace event = . if first_year > first_event - 2 & !missing(first_event) /*Psuedo drop these observations*/

gen event_tm2 = 0 if !missing(event)
replace event_tm2 = 1 if F2.event == 1 & !missing(event)
gen event_tm1 = 0 if !missing(event)
replace event_tm1 = 1 if F.event == 1 & !missing(event)
gen event_t = 0 if !missing(event)
replace event_t = 1 if event == 1 & !missing(event)
gen event_tp1 = 0 if !missing(event)
replace event_tp1 = 1 if L.event == 1 & !missing(event)
gen event_tp2 = 0 if !missing(event)
replace event_tp2 = 1 if L2.event == 1 & !missing(event)
gen event_tp3 = 0 if !missing(event)
replace event_tp3 = 1 if year > first_event + 2 & !missing(event)




winsor2 roa q sales_growth, cuts(1 99) replace

foreach x in roa q sales_growth {
	egen z`x' = std(`x') if year <= 2018 & !missing(connect_num) & ind_contracts > 0 & !missing(ind_contracts) & !missing(contract_flag) & !missing(event_tm2) & !missing(event_tm1) & !missing(event_t) & !missing(event_tp1) & !missing(event_tp2) & !missing(event_tp3)
}




/* BASELINE */

reghdfe contract_flag c.event_tm2 c.event_tm1 c.event_t c.event_tp1 c.event_tp2 c.event_tp3 if year <= 2018 & !missing(connect_num) & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store z1



local i = 1
foreach x in zroa zsales_growth zq {
	reghdfe contract_flag c.event_tm2##c.`x' c.event_tm1##c.`x' c.event_t##c.`x' c.event_tp1##c.`x' c.event_tp2##c.`x' c.event_tp3##c.`x' if year <= 2018 & !missing(connect_num) & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
	estimates store a`i'
	local ++i
}


sum contract_flag if _est_z1 == 1
sum contract_flag if _est_a1 == 1
sum contract_flag if _est_a2 == 1
sum contract_flag if _est_a3 == 1



#delimit ;
esttab z1 using output.csv, 
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2
	replace;
esttab a1 a2 a3 output.csv, 
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2
	append;
#delimit cr